package com.online.taxi.order.implementation.redissonClientRed;

import com.online.taxi.order.implementation.LockKey;
import org.redisson.RedissonRedLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * @Author:jiangqikun
 * @Date:2022/8/21 14:59
 **/

public class RedissonClientRedUse {

    @Autowired
    @Qualifier("redissonRed1")
    private RedissonClient redissonRed1;

    @Autowired
    @Qualifier("redissonRed2")
    private RedissonClient redissonRed2;

    @Autowired
    @Qualifier("redissonRed3")
    private RedissonClient redissonRed3;


    public void use(String orderId, String driverId) {
        // 生成 key
        LockKey key = LockKey.builder()
                .driverId(driverId)
                .orderId(orderId)
                .build();
        //红锁 redis son
        RLock rLock1 = redissonRed1.getLock(orderId.intern());
        RLock rLock2 = redissonRed2.getLock(orderId.intern());
        RLock rLock3 = redissonRed3.getLock(orderId.intern());
        RedissonRedLock rLock = new RedissonRedLock(rLock1,rLock2,rLock3);

        try {
            // 此代码默认 设置key 超时时间30秒，过10秒，再延时
            rLock.lock();
            // 执行业务代码


        } finally {
            rLock.unlock();
        }
    }

}
